package com.swing.sky.web.generator.config;


import com.swing.sky.web.generator.domain.Module;
import com.swing.sky.web.generator.util.PropertyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author swing
 */
public class GenConfig {
    public static final String MAIN = "main";
    public static final String TEST = "test";
    public static final String SRC = "src";
    public static final String RESOURCES = "resources";


    /**
     * 获取生成代码模块信息
     *
     * @return 结果
     */
    public static List<Module> getModules() {
        List<Module> modules = new ArrayList<>();
        String modulesStr = PropertyUtils.readGen("modules");
        String[] moduleNames = modulesStr.split(",");
        for (String moduleName : moduleNames) {
            modules.add(getModule(moduleName));
        }
        return modules;
    }

    /**
     * 通过模块名获取一个新的模块实例
     */
    public static Module getModule(String moduleName) {
        Module module = new Module();
        module.setModuleName(moduleName);
        module.setExtension(PropertyUtils.readGen(moduleName + "." + "extension"));
        module.setSuffix(PropertyUtils.readGen(moduleName + "." + "suffix"));
        module.setTemplateUrl(PropertyUtils.readGen(moduleName + "." + "templateUrl"));
        String type = PropertyUtils.readGen(moduleName + "." + "type");
        //如果是源码文件
        if (type.endsWith(SRC)) {
            module.setPackageName(getBasicPackage() + "." + PropertyUtils.readGen(moduleName + "." + "packageName"));
            //绝对路径=输出路径名+打包工具内部源码路径+包名
            if (type.startsWith(MAIN)) {
                module.setAbsolutePath(getExportUrl() + "\\" + getJavaPath() + "\\" + module.getPackageName().replace(".", "\\"));
            } else {
                module.setAbsolutePath(getExportUrl() + "\\" + getTestJavaPath() + "\\" + module.getPackageName().replace(".", "\\"));
            }
        }
        //如果是资源文件
        if (type.endsWith(RESOURCES)) {
            //绝对路径=输出路径名+打包工具内部源码路径+包名
            if (type.startsWith(MAIN)) {
                module.setAbsolutePath(getExportUrl() + "\\" + getResourcesPath() + "\\" + PropertyUtils.readGen(moduleName + "." + "packageName"));
            } else {
                module.setAbsolutePath(getExportUrl() + "\\" + getTestResourcesPath() + "\\" + PropertyUtils.readGen(moduleName + "." + "packageName"));
            }
        }
        return module;
    }

    public static String getAuthor() {
        return PropertyUtils.readSky("author");
    }

    public static String getBasicPackage() {
        return PropertyUtils.readSky("basicPackage");
    }

    public static String getPackageTool() {
        return PropertyUtils.readGen("packageTool");
    }

    public static String getJavaPath() {
        return PropertyUtils.readGen(getPackageTool() + ".java.path");
    }

    public static String getTestJavaPath() {
        return PropertyUtils.readGen(getPackageTool() + ".test.java.path");
    }

    public static String getResourcesPath() {
        return PropertyUtils.readGen(getPackageTool() + ".resources.path");
    }

    public static String getTestResourcesPath() {
        return PropertyUtils.readGen(getPackageTool() + ".test.resources.path");
    }

    public static String getExportUrl() {
        return PropertyUtils.readSky("exportUrl");
    }


}
